package com.twu.biblioteca.Commands;

import com.twu.biblioteca.Library;
import com.twu.biblioteca.LogInSystem;

import java.util.Scanner;

public class ReserveBookCommand implements Command {
    @Override
    public String execute(Library lib, Scanner scanner, LogInSystem logInSystem) {
        if (!logInSystem.isUserLoggedIn())
            return "Please talk to a librarian. Thank you.";

        printAvailableBooks(lib);
        try {
            if (lib.attemptBookReservation(scanner.nextInt()))
                return "Thank You! Enjoy the book.";
        }
        catch (Exception ex) {
            return "Select a valid option!!";
        }
        return "Sorry we don't have that book yet.";
    }

    private void printAvailableBooks(Library lib) {
        String retString = "Books\n\n";
        String[] books = lib.getBooksArray();
        for (int i=0; i<books.length; i++) {
            retString += (i + " - " + books[i] + "\n");
        }
        System.out.print(retString);
    }
}
